﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownFileSplitter
{
    internal class GlobalSetting
    {
        // 单例实例
        private static GlobalSetting _instance;
        // 锁对象，用于线程安全
        private static readonly object _lock = new object();
        // 存储文件的路径
        private const string SettingsFilePath = "settings.json";

        public string StorePlace { get; set; }
        public string MinIOUrl { get; set; } = "http://192.168.2.231:9002/zhishiku";
        public string MinIOAccessKey { get; set; } = "Ia5Ee7KqKPk5buWPnDIr";
        public string MinIOSecretKey { get; set; } = "qqWWCYM8Hf99rzfMNaHVMHuO0c8jgXagmKmLG9CU";
        public string AIMethod { get; set; } = "Ollama";
        public string OllamaUlr { get; set; } = "http://192.168.3.101:11434";
        public string OllamaModelName { get; set; } = "qwen2.5:32b";
        public string OpenRouterAPIKey { get; set; } = "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";
        /// <summary>
        /// Umi-OCR第三方exe的路径
        /// </summary>
        public string UmiOCRPath { get; set; } = @"D:\Workspace\CSharp\Umi-OCR_Paddle_v2.1.5\Umi-OCR.exe";

        public string MeilisearchUrl { get; set; } = "http://192.168.2.203:7700";
        public string MeilisearchAPIKey { get; set; } = "4eaf6543-6953-44ff-b2a7-48f966e64bba";

        // 单例模式的访问点
        public static GlobalSetting Instance
        {
            get
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = LoadFromFile();
                    }
                    return _instance;
                }
            }
        }

        // 从本地文件加载设置
        private static GlobalSetting LoadFromFile()
        {
            if (File.Exists(SettingsFilePath))
            {
                try
                {
                    var json = File.ReadAllText(SettingsFilePath);
                    return System.Text.Json.JsonSerializer.Deserialize<GlobalSetting>(json);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"加载设置时出错: {ex.Message}");
                }
            }
            return new GlobalSetting();
        }

        // 将设置保存到本地文件
        public void SaveToFile()
        {
            try
            {
                var json = System.Text.Json.JsonSerializer.Serialize(this);
                File.WriteAllText(SettingsFilePath, json);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存设置时出错: {ex.Message}");
            }
        }
    }
}
